package logic

import (
	"context"
	"encoding/json"
	"teacher1/model/mysql"
	"teacher1/model/redis"
	"teacher1/studentrpc/internal/svc"
	"teacher1/studentrpc/studentrpc"
	"time"

	"github.com/zeromicro/go-zero/core/logx"
)

type StudentListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewStudentListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *StudentListLogic {
	return &StudentListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *StudentListLogic) StudentList(in *studentrpc.StudentListRequest) (*studentrpc.StudentListResponse, error) {
	// todo: add your logic here and delete this line
	var students []mysql.Student
	mysql.DB.Debug().Where("teacherId=?", in.TeacherId).Find(&students)
	var lists []*studentrpc.StudentList
	redis.Rdb.Get("studentList")
	for _, student := range students {
		list := studentrpc.StudentList{
			Number:    student.Number,
			IdCard:    student.IdCard,
			RealName:  student.RealName,
			Mobile:    student.Mobile,
			Job:       student.Job,
			HeadImg:   student.HeadImg,
			TeaCherId: int64(student.ID),
		}
		lists = append(lists, &list)
	}
	marshal, _ := json.Marshal(lists)
	redis.Rdb.Set("studentList", marshal, time.Minute*10)

	return &studentrpc.StudentListResponse{List: lists}, nil
}
